//
//  ClosureViewController.swift
//  Swift 3.0 基础语法
//
//  Created by YYY on 2017/7/18.
//  Copyright © 2017年 成品家（北京）网路科技有限公司. All rights reserved.
//

import UIKit

/*
 函数是特殊的闭包
 闭包相当于oc中的block
 */
class ClosureViewController: CommonViewController {

    //定义闭包变量 可选闭包
    var optionalClosure: (() -> ())?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        print(sum(x: 10, y: 20))
        
        let total = sum
        
        print(total(20, 80))
        
        //{}表示代码块 定义闭包1
        //没有参数，没有返回值，可以省略，in也可以省略
        let number = {
            print("hello")
        }
        number()
        
        
        //闭包格式：{形参列表 -> 返回值类型 in 实现代码}
        //带参数，没有返回值的闭包2
        let b1 = {(x: Int) -> () in
            
            print(x)
        }
         b1(230)
        
        //带参数，有返回值的闭包3
        let b2 = {(x: Int) -> Int in
            return x + 80
        }
        print(b2(30))
      
        
        //调用闭包1
        closure1 { (completion) in
            print(completion)
        }
        // Do any additional setup after loading the view.
    }

    func sum(x: Int,y: Int) -> Int
    {
        return x + y
    }
    
    //可选闭包的回调
    func optionalClosureBlock()
    {
        optionalClosure?()
    }
    
    //闭包形式1 数组参数 @escaping (参数类型) -> ()
    func closure1(completion: @escaping ([String])-> ())
    {
        let array = ["3","2","1"]
        completion(array)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
